<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>login</title>
    <script type="text/javascript" src="./js/jquery-1.8.0.js"></script>
  </head>
  <body>
  	  
    <html:form action="/user.do?method=login">
    <table width="40%" align="center" height="200">
	    <tr>
		     <td colspan="2">
		     <html:errors/> 	
		     </td>
	    </tr>
    </table>
    
    <table border="1" align="center" width="40%" onkeydown="keydown(event)">
    	<tr>
    	 	<td>
    	 		用户名
    	 	</td>
    	 	<td>
    	 		<html:text property="user.userName"> </html:text>
    	 	 
    	 	</td>
    	</tr>
    	<tr>
    		<td>
    			密码
    		</td>
    		<td>
    			<html:password property="user.userPwd" size="21"> </html:password>
    			 
    	 	</td>
    	</tr>
    	<tr>
    		<td>
    			 验证码：
    		</td>
    		<td>
			 <input type="text" size="20" id="vcode" name="vcode" maxlength="4" id="vcode"/>
			 <img src="img.jsp?Math.random()" id="imgcode" name="imgcode" />
			 <a href="javascript:reloadImage('img.jsp');">看不清</a>
			 
    		</td>
    	</tr>
    	<tr>
    		<td align="center" colspan="2">
    			<input type="button" onclick="loginVlidate();" value="登陆">
    		</td>
    		 
    	</tr>
    </table>
    <div id="errorInfo"></div>
    </html:form>
  </body>
  
   <script>
	/*在文字上使验证码刷新 */ 
	function reloadImage(url)
	{ 
		var img = document.getElementById("imgcode"); 
		img.src = url+"?" + Math.random(); 
	}
 
  function keydown(event)
  {
     if(event.keyCode==13)
  		loginVlidate()
  }
  function loginVlidate()
  {
  
  	var vcode=document.getElementById("vcode").value;
   	var value1=document.getElementById("user.userName").value;
 	var value2=document.getElementById("user.userPwd").value;
 	 
 	if(value1=="")
 	{
 		alert("用户名不能为空");
 		return false;
 	}
 	if(value2=="")
 	{
 		alert("密码不能为空");
 		return false;
 	}
 	if(vcode=="")
 	{
 		alert("验证码不能为空");
 		return false;
 	}
    $.ajax({    
              url:'user.do?method=loginVlidate',
              type: "post",  
              data:{sname:value1,spassword:value2,vcode:vcode},        
              error:function(){                    
              	alert("error occured!!!");                    
              },                    
              success:function(data){  
	              if(data=="0")
	              {
	             	 alert("用户名和密码不正确 ");
	             	 document.getElementById("user.userName").value="";
	             	 document.getElementById("user.userPwd").value="";
	             	 return false;
	              }
	              if(data=="1")
	              {
	                    alert("验证码不正确！");
	             		reloadImage('img.jsp');
	             		document.getElementById("vcode").value="";
	             		return false;
	              }
	              else
	              {
	                
	                document.forms[0].submit();
	              }
	                                
                        
              }                                     
      });  
   
  }
  </script>
</html>